﻿@page "/blog/modify/{Id:int}"

@using Blog.MVP.Blazor.SSR.Models

@inject HttpClient Http
@inject NavigationManager NavManager
@inject BlogService BlogService

@using  Blog.MVP.Blazor.SSR.Pages.Post.component

<h1>编辑</h1>

<Editor BlogArticle="BlogArticle" OnSaveCallback="OnSaveAsync"></Editor>


<div class="text-danger">
    @_errmsg
</div>

@code {
    [Parameter]
    public int Id { get; set; }

    private BlogArticle BlogArticle { get; set; }

    private string _errmsg = "";



    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            BlogArticle = (await BlogService.GetBlogByIdForMVP(Id)).response;

            StateHasChanged();
        }
    }

    private async Task OnSaveAsync(BlogArticle blogArticle)
    {
        BlogArticle = blogArticle;

        var result = await BlogService.UpdateBlog(BlogArticle);

        if (result.IsSuccessStatusCode)
        {
            NavManager.NavigateTo("/blog/list");
        }
        else
        {
            _errmsg = "保存失败! 错误原因：" + result.ReasonPhrase + "。请重试或登录";
        }
    }

}